

#include	"SysConfig.h"
#include	"Define.h"
#include	"Model_Option.h"
#include	"Function.h"
#include	"Key.h"
#include	"Display.h"
#include	"UI_Main.h"
#ifdef  MIC
#include	"MIC.h"
#endif

#ifdef  BLUETOOTH
#include	"Bluetooth.h"
#ifdef  MOVON
#include	"Movon.h"
#endif
#endif

#ifdef  BLUETOOTH

#ifdef  MOVON
extern MV_HF_STATUS ucMV_HFS_Status;
#endif

extern unsigned char ucBT_DeviceCnt;
extern unsigned char ucBT_Function_Active;
extern UI_MENU_ID ucMenuID;
extern unsigned char ucMENU_CurNo;
extern unsigned char ucPBAP_CallHistoryCnt;
extern unsigned char ucBT_UserReject;

extern unsigned char BT_ConnectRequest(unsigned char ucOrder);
extern void BT_SetProcess(unsigned char ucMode);
extern void BT_AVRCP_Command(BT_AVRCP ucCmd);
extern void BT_BluetoothOnOff(unsigned char ucDes);
extern void DISP_SetBlink(void);
extern void MENU_SetID(UI_MENU_ID ucID);
extern void MENU_SetID_10Sec(UI_MENU_ID ucID);
extern unsigned char BT_PairingCancel(void);
extern unsigned char BT_ConnectingCancel(void);
extern void AUDIO_BeepOutStart(void);
extern unsigned char BT_ConnectStatus(void);
extern unsigned char MENU_GetID(void);
extern void AUDIO_SetMute(unsigned char ucDes);
extern unsigned char BT_NumberDial_CallHistory(void);

unsigned char UI_BT_ShortKey_Main(KEY_ID ucKey)
{
	unsigned char ucRet = 0;

	switch  (ucKey)
	{
		case KEY_TUNE_UP:
			if  (FUNC_GetCurrentFunction() == FUNC_A2DP)
			{
				BT_AVRCP_Command(BT_AVRCP_NEXT);
				ucRet = 1;
			}
			break;
		case KEY_TUNE_DOWN:
			if  (FUNC_GetCurrentFunction() == FUNC_A2DP)
			{
				BT_AVRCP_Command(BT_AVRCP_PREVIOUS);
				ucRet = 1;
			}
			break;
		case KEY_NUM1:
			if  (FUNC_GetCurrentFunction() == FUNC_A2DP)
			{
				BT_AVRCP_Command(BT_AVRCP_PLAY_PAUSE);
				ucRet = 1;
			}
			break;
		case KEY_CALL:
			if (ucBT_Function_Active == 0)
			{
				break;
			}
		#ifdef  MIC
			if  (MIC_GetHostMicStatus() == MIC_ON)
			{
				MIC_SetHostMicStatus(MIC_OFF);
			}
		#endif
			
			if  (MENU_GetID() == MENU_BT_RECENT_ID)
			{
				if  (BT_NumberDial_CallHistory() == 1)
				{
					MENU_SetID(MENU_BT_RECENT_ID);
					ucRet = 1;
				}
			}			
			else
			{
				switch  (ucMV_HFS_Status)
				{
					case MV_HFS_INCOMING:
						MV_Set_AudioConnectAG();
						BT_SetProcess(BT_PRS_ANSWER_CALL);
						ucRet = 1;
						break;
					case MV_HFS_ACTIVE_CALL:
						BT_SetProcess(BT_PRS_CALL_TRANS);
						ucRet = 1;
						break;
					case MV_HFS_CONNECTABLE:
						if  (BT_ConnectRequest(0) == 1)
						{
							MENU_SetID(MENU_BT_CONNETING_ID);
							ucRet = 1;
						}
						break;
					case MV_HFS_CONNECTED:
						if  (ucPBAP_CallHistoryCnt != 0)
						{
							MENU_SetID(MENU_BT_RECENT_ID);
							DISP_SetTempDisplayMode(DISP_BT_RECENT_CALLS);
							ucMENU_CurNo = 0;
							ucRet = 1;
						}
						break;
				}
			}
			break;
		case KEY_POWER:
			if  (ucMV_HFS_Status == MV_HFS_ACTIVE_CALL)
			{
				BT_SetProcess(BT_PRS_MIC_MUTE);
				DISP_SetBlink();
				ucRet = 1;
			}
			ucRet = 1;
			break;
		case KEY_HANG_UP:
			{
				switch  (ucMV_HFS_Status)
				{
					case MV_HFS_OUTGOING:
						BT_SetProcess(BT_PRS_HANDUP_CALL);
						DISP_SetTempDisplayMode(DISP_BT_CALL_END);
						ucRet = 1;
						break;
					case MV_HFS_INCOMING:
						BT_SetProcess(BT_PRS_REJECT_CALL);
						ucBT_UserReject = 1;
						DISP_SetTempDisplayMode(DISP_BT_CALL_REJECT);
						ucRet = 1;
						break;
					case MV_HFS_ACTIVE_CALL:
						BT_SetProcess(BT_PRS_HANDUP_CALL);
						DISP_SetTempDisplayMode(DISP_BT_CALL_END);
						ucRet = 1;
						break;
					case MV_HFS_3WC_WAITING:
						ucRet = 1;
						break;
					case MV_HFS_3WC_HOLD:
						ucRet = 1;
						break;
					case MV_HFS_3WC_MULTI:
						ucRet = 1;
						break;
				}

				if  (BT_PairingCancel() == 1)
				{
					DISP_SetTempDisplayMode(DISP_BT_PAIR_CANCEL);
				}
				if  (BT_ConnectingCancel() == 1)
				{
			//		DISP_SetTempDisplayMode(DISP_BT_CONNECT_CANCEL);
				}
			}
			break;
	}

	return(ucRet);
}
	
unsigned char UI_BT_LongKey_Main(KEY_ID ucKey)
{
	unsigned char ucRet = 0;

	switch  (ucKey)
	{
		case KEY_TUNE_UP:
			if  (FUNC_GetCurrentFunction() == FUNC_A2DP)
			{
				BT_AVRCP_Command(BT_AVRCP_NEXT);
				ucRet = 1;
			}
			break;
		case KEY_TUNE_DOWN:
			if  (FUNC_GetCurrentFunction() == FUNC_A2DP)
			{
				BT_AVRCP_Command(BT_AVRCP_PREVIOUS);
				ucRet = 1;
			}
			break;
		case KEY_CALL:
			{
				switch	(ucMV_HFS_Status)
				{
					case MV_HFS_INCOMING:
						MV_Set_AudioConnectAG();
						BT_SetProcess(BT_PRS_ANSWER_CALL);
						ucRet = 1;
						break;
					case MV_HFS_ACTIVE_CALL:
						BT_SetProcess(BT_PRS_CALL_TRANS);
						ucRet = 1;
						break;
					case MV_HFS_CONNECTED:
						if  (MENU_GetID() == MENU_BT_RECENT_ID)
						{
							if  (BT_NumberDial_CallHistory() == 1)
							{
								MENU_SetID(MENU_BT_RECENT_ID);
								ucRet = 1;
							}
						}			
						else
						{
							if  (ucPBAP_CallHistoryCnt != 0)
							{
								MENU_SetID(MENU_BT_RECENT_ID);
								DISP_SetTempDisplayMode(DISP_BT_RECENT_CALLS);
								ucMENU_CurNo = 0;
								ucRet = 1;
							}
						}
			//			BT_SetProcess(BT_PRS_REDIAL);
			//			DISP_SetTempDisplayMode(DISP_BT_REDIAL);
			//			ucRet = 1;
			//			AUDIO_BeepOutStart();
						break;
				}
						
				if  (ucBT_Function_Active == 1)
				{
					if	(BT_ConnectStatus() == 0)
					{
						if  (ucBT_DeviceCnt == BT_DEVICE_NUM)
						{
							MENU_SetID_10Sec(MENU_BT_DEL_LIST_ID);
							DISP_SetTempDisplayMode(DISP_BT_LIST_FULL);
						}
						else
						{
							BT_PairingRequest();
							MENU_SetID(MENU_BT_PAIR_ACTIVE_ID);
						}
						AUDIO_BeepOutStart();
					}
				}	
			}
			break;
		case KEY_HANG_UP:
			{
				switch  (ucMV_HFS_Status)
				{
					case MV_HFS_OUTGOING:
						BT_SetProcess(BT_PRS_HANDUP_CALL);
						DISP_SetTempDisplayMode(DISP_BT_CALL_END);
						ucRet = 1;
						break;
					case MV_HFS_INCOMING:
						BT_SetProcess(BT_PRS_REJECT_CALL);
						ucBT_UserReject = 1;
						DISP_SetTempDisplayMode(DISP_BT_CALL_REJECT);
						ucRet = 1;
						break;
					case MV_HFS_ACTIVE_CALL:
						BT_SetProcess(BT_PRS_HANDUP_CALL);
						DISP_SetTempDisplayMode(DISP_BT_CALL_END);
						ucRet = 1;
						break;
					case MV_HFS_CONNECTED:
						ucBT_Function_Active = 0;
						BT_BluetoothOnOff(0);
						DISP_SetTempDisplayMode(DISP_BT_MODE_SET);
						AUDIO_BeepOutStart();
						ucRet = 1;
						break;
				}

				if  (BT_PairingCancel() == 1)
				{
					DISP_SetTempDisplayMode(DISP_BT_PAIR_CANCEL);
				}
			}
			break;
	}

	return(ucRet);
}

/*//////////////////////////////////////////////////////////////////////////
Title : unsigned char UI_BT_Key_Main(UI_KEY_ID ucKey, unsigned char ucLongKey)
ucRet : 
	0 : Not Action
	1 : Any Action
//////////////////////////////////////////////////////////////////////////*/
unsigned char BT_Key_Main(KEY_ID ucKey, unsigned char ucLongKey)
{
	unsigned char ucRet = 0;

	if  (ucBT_Function_Active == 0)
	{
		return(ucRet);
	}
	if  (ucLongKey == 0)
	{
		ucRet = UI_BT_ShortKey_Main(ucKey);
	}
	else
	{
		ucRet = UI_BT_LongKey_Main(ucKey);
	}

	return(ucRet);
}

#endif
